package com.system.controller;
import com.system.entity.Admin;
import com.system.service.LoginService;
import com.system.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;


/**
 * 登录主页控制器
 * @author llq
 *
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 跳转登陆页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(ModelAndView model) {
        //通过ModelAndView中的方法跳转到jsp页面
        model.setViewName("login");
        return model;
    }


    /**
     * 跳转欢迎欢迎页面
     *
     * @param model
     * @return
     */

    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public ModelAndView welcome(ModelAndView model) {
        //通过ModelAndView中的方法跳转到jsp页面
        model.setViewName("system/welcome");
        return model;
    }

    /**
     * 登录表单提交
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody//注解的意思是按json的格式返回
    public ModelAndView login(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password,
            ModelAndView model,
            HttpServletRequest request

    ) {
        //登录用户名：admin,密码：123456
        String password1=MD5Util.MD5(password);//解密MD5加密进行登录
        Admin admin = loginService.findByAdminName(username, password1);

        if (admin != null) {
            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);
            model.setViewName("system/main");
            return model;
        } else {
            request.setAttribute("errorMsg", "用户名或密码有误，请从新输入!!");
            model.setViewName("login");
            return model;

        }
    }

    /**
     * 跳转个人中心页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public ModelAndView person(ModelAndView model) {

        model.setViewName("system/person");
        return model;
    }

    /**
     * 修改个人信息
     */
    @RequestMapping(value = "/updateperson", method = RequestMethod.POST)
    public String updatePerson(Admin admin, ModelAndView model, HttpServletRequest request) {



        int result = loginService.updateperson(admin);

        if (result > 0) {
            request.setAttribute("successMsg", "修改信息成功!!");
            model.setViewName("/system/main");
            return null;

        } else {
            return null;
        }
    }
    /**
     * 注册
     */
    @RequestMapping(value = "/doRegister")
    @ResponseBody
    public String doRegister(Admin admin) {
    try
    {
        admin.setPassword(MD5Util.MD5(admin.getPassword()));//使用MD5加密
        loginService.doRegister(admin);
        return "ok";
    } catch(
    Exception e)

    {
        e.printStackTrace();
    }
    return null;
}
/**
 *
 * 判断用户名是否存在
 */
@ResponseBody
@RequestMapping(value="/adminValidate", method=RequestMethod.POST)
public String adminValidate(Admin admin) {

    try {
        Admin result=loginService.adminValidate(admin);
        if(result!=null){
            return "ok";
        }
        else{
            return "no";
        }

    }
    catch (Exception e){
        e.printStackTrace();
    }
    return null;
}


/**
 * 小程序进行登录功能
 *
 */
    @RequestMapping(value = "/wxlogin", method = {RequestMethod.POST})
    @ResponseBody
    public String wxlogin(
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password,
            ModelAndView model,
            HttpServletRequest request

    ) {
        //登录用户名：admin,密码：123456
        String password1=MD5Util.MD5(password);//解密MD5加密进行登录
        Admin admin1 = loginService.findByAdminName(username, password1);
        if (admin1 != null) {
            HttpSession session = request.getSession();
            session.setAttribute("admin", admin1);
           return "sd";
        }
        return "bx";
    }



}






